package com.read.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 日期工具类
 * @author ksir
 *
 */
public class DateUtils {
	public static final int YYYY_MM_DD = 1;
	public static final int YYYY_MM_DD_HH_MM_SS = 2;
	public static final String YYYY_MM_DD_PATTERN ="^\\d{4}-\\d{2}-\\d{2}$";
	public static final String YYYY_MM_DD_HH_MM_SS_PATTERN ="^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}$";
	
	public static boolean isYYYY_MM_dd(String s){
		Matcher m = Pattern.compile(YYYY_MM_DD_PATTERN).matcher(s);
		if(m.find()){
			return true;
		}
		return false;
	}
	public static boolean isYYYY_MM_DD_HH_MM_SS(String s){
		Matcher m = Pattern.compile(YYYY_MM_DD_HH_MM_SS_PATTERN).matcher(s);
		if(m.find()){
			return true;
		}
		return false;
	}
	public static int getFlag(String s){
		if(isYYYY_MM_dd(s)){
			return YYYY_MM_DD;
		}
		if(isYYYY_MM_DD_HH_MM_SS(s)){
			return YYYY_MM_DD_HH_MM_SS;
		}
		return -1;
	}
	
	public static Date parse(String format, String date){
		DateFormat dt = new SimpleDateFormat(format);
		try {
			return (Date)dt.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	
	public static void main(String[] args) {
		String s = "1992-10-10 20:02:11";
		System.out.println(parse("yyyy-MM-dd HH:mm:SS",s));
	}
}
